package com.itheima;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.*;

public class MyDateTypeHandler extends BaseTypeHandler<Date> {


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
        //转换成毫秒值存储到数据库
        ps.setString(i,parameter.getTime()+"");
    }

    @Override
    public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
      //把数据库中的值转换成 date类型
        return new Date(rs.getLong(columnName));
    }

    @Override
    public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return new Date(rs.getLong(columnIndex));
    }

    @Override
    public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return cs.getDate(columnIndex);
    }
}
